package cn.cy.structure.d02Dynamic.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class MyInvocationHandler implements InvocationHandler {
    
    private UserService userService;

    public MyInvocationHandler(UserService userService) {
        this.userService = userService;
    }

    /**
     * @param proxy 内存中的代理实例 $proxy0
     * @param method 内存代理实例中class.forName("被代理类").getMethod("目标方法") 即被代理的类的方法对象
     * @param args 方法参数
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();

        method.invoke(userService, args);

        after();
        
        return null;
    }

    private void before() {
        System.out.println("通过JDK动态代理开启事务...");
    }

    private void after() {
        System.out.println("通过JDK动态代理结束事务...");
    }

}
